#!/usr/bin/perl
use File::Path;
use DBI;

# 判断是否存在临时trigger [id]目录
my $path = '/data/graph/trigger-$ARGV[0]';
if(-e $path) { rmtree($path); }
mkdir($path);

my $stime = `date +%Y%m%d`; chop($stime); $stime .= '1000';
if( length($stime) != 12 ) { print "Error get date"; exit; }

# 定义图形时间区间
#my $period = 86400;    # 24 hours
my $period = 21600;    # 6 hours


# 定义登陆信息
my $login = 'Admin';  # Zabbix Web User
my $pass = 'zabbix'; # Zabbix Web User Password, must be URL Encoded

my $cook = "/tmp/cookie";
my $dsn = 'DBI:mysql:zabbix:localhost'; # Connect MySQL DB "zabbix" on localhost
my $db_user_name = 'zabbix'; # MySQL DB user
my $db_password = 'zabbix'; # MySQL DB user password

my $dbh = DBI->connect($dsn, $db_user_name, $db_password);
#my $sth = $dbh->prepare(qq{select a.name,a.hsize,a.vsize, b.resourceid, b.width, b.height,b.x,b.y from screens a,screens_items as b where a.screenid=b.screenid and a.templateid<=>NULL order by a.name});
#$sth->execute();
#my %screens;

# Get all graphs by using curl

#while (my ($name,$hsize,$vsize, $id,$width,$height,$x,$y) = $sth->fetchrow_array())
#{
#    if(length($id) > 2){
#        #print "$id => $ids\n";
#        my $p = "$path/$name.$hsize.$vsize.$y.$x.$id.png";
my $strcomm  = `curl  -c $cook -b $cook -d "request=&name=$login&password=$pass&autologin=1&enter=Sign+in"  localhost/zabbix/index.php`;
#        $strcomm  = `curl  -b $cook -F  "graphid=$id" -F "period=$period" -F "stime=$stime" -F "width=$width" -F "height=$height" localhost/zabbix/chart2.php > $p`;
#    }
#}
my $p = "$path/itemid-$ARGV[0].png";


# 如果知道 itemid , 用chart.php
$strcomm = `curl  -b $cook -F "graphid=$ARGV[0]" localhost/zabbix/chart.php > $p`;

# 如果知道graphid, 用chart2.php 
$strcomm = `curl  -b $cook -F "graphid=$ARGV[0]" localhost/zabbix/chart2.php > $p`;

# 用chart3.php 未知
#$strcomm = `curl  -b $cook -F "graphid=$ARGV[0]" localhost/zabbix/chart3.php > $p`;

# 如果知道 Trigger id , 用chart4.php,柱状图表示一段时间的可用率
$strcomm = `curl  -b $cook -F "triggerid=$ARGV[0]" localhost/zabbix/chart4.php > $p`;

# 如果知道 service id 并且是做饼图 , 用chart5.php
$strcomm = `curl  -b $cook -F "graphid=$ARGV[0]" localhost/zabbix/chart5.php > $p`;

# 如果知道 graph id 并且是做饼图 , 用chart6.php
$strcomm = `curl  -b $cook -F "graphid=$ARGV[0]" localhost/zabbix/chart5.php > $p`;

# chart7.php 未知 ，好像是饼图(1h)
#$strcomm = `curl  -b $cook -F "graphid=$ARGV[0]" localhost/zabbix/chart5.php > $p`;


exit ;